package com.example.servicedemo;

import java.util.ArrayList;

public class RollingAverage {
	
	private ArrayList<Object> mList;
	private int mMaxSize;
	
	RollingAverage(int size) {
		mMaxSize = size;
		mList = new ArrayList<Object>();
	}
	
	public double calcAvgDouble(double value) {
		double sum = 0;
		
		if(mList.size() < mMaxSize) {
			mList.add((Object) value);
		} else {
			// remove the value at the top of the list, then insert the new value
			mList.remove(0);
			mList.add((Object) value);
		}
		
		for(Object d : mList) {
			sum += (Double) d;
		}
		
		return (sum / ((double)mList.size())); 
	}
	
	public int calcAvgInteger(int value) {
		int sum = 0;
		
		if(mList.size() < mMaxSize) {
			mList.add((Object) value);
		} else {
			// remove the value at the top of the list, then insert the new value
			mList.remove(0);
			mList.add((Object) value);
		}
		
		for(Object d : mList) {
			sum += (Integer) d;
		}
		
		return (sum / mList.size()); 
	}
	
	public double getLastDouble() {
		if(mList.size() == 0) return -1;
		return (Double) mList.get(mList.size() - 1);
	}
	
	public int getLastInteger() {
		if(mList.size() == 0) return -1;
		return (Integer) mList.get(mList.size() - 1);
	}

}
